#include "control/cairdim.h"
#include "caudiocontrol.h"

CAudioControl::CAudioControl(QObject *parent) :
    QObject(parent)
{
}

void CAudioControl::insertDeviceObject(Json_DEVICE_OBJECT *device)
{
    if(device->_usrcontrol == NULL)
        return;

    CVoiceObject *newVoice = new CVoiceObject();
    newVoice->_pid = device->_pid.toInt();
    newVoice->_device = device;
    newVoice->_did = device->_id.toInt();
    newVoice->_name = device->_name;
    newVoice->_floor = device->_usrcontrol->_floor.toInt();
    newVoice->_location = device->_usrcontrol->_location;

    QString _type = device->_type;
    if(_type == "switch")
    {
        newVoice->_type = "light";
        newVoice->_funtion = "switch";

        ADApi::MediasRegistry(newVoice->_did,newVoice->_pid,newVoice->_name,newVoice->_type,newVoice->_funtion,
                           newVoice->_floor,newVoice->_location);
    }
    else if(_type == "dim")
    {
        newVoice->_type = "light";
        newVoice->_funtion = "switch;dim";

        ADApi::MediasRegistry(newVoice->_did,newVoice->_pid,newVoice->_name,newVoice->_type,newVoice->_funtion,
                           newVoice->_floor,newVoice->_location);
    }
    else if(_type == "scene")
    {
        newVoice->_type = "scene";
        newVoice->_funtion = "scene";
        newVoice->_name = newVoice->_name.remove("场景");
        newVoice->_name = newVoice->_name.remove("模式");

        ADApi::ScenesRegistry(newVoice->_did,newVoice->_pid,newVoice->_name,
                           newVoice->_floor,newVoice->_location);        
    }
    else if(_type == "window")
    {
        newVoice->_type = "curtain";
        newVoice->_funtion = "move";

        ADApi::MediasRegistry(newVoice->_did,newVoice->_pid,newVoice->_name,newVoice->_type,newVoice->_funtion,
                           newVoice->_floor,newVoice->_location);
    }
    else if(_type == "air_dim")
    {
        newVoice->_type = "air";
        newVoice->_funtion = "temp;switch;model;wind";

        ADApi::MediasRegistry(newVoice->_did,newVoice->_pid,newVoice->_name,newVoice->_type,newVoice->_funtion,
                           newVoice->_floor,newVoice->_location);

    }
    else if(_type == "air_mode")
    {
        newVoice->_type = "";
        newVoice->_funtion = "model";
        _audioMap.insert(newVoice->_did,newVoice);
    }
    else if(_type == "air_wind")
    {
        newVoice->_type = "";
        newVoice->_funtion = "air_wind";
        _audioMap.insert(newVoice->_did,newVoice);
    }
    else if(_type == "label")
    {

    }
    else if(_type == "media")
    {

    }

    if(newVoice->_did>0 && !newVoice->_type.isEmpty()){
        _audioMap.insert(newVoice->_did,newVoice);

        qDebug() << "Audio Control: regist object " << newVoice->_type
                    << " - " << newVoice->_funtion;
    }
}

void CAudioControl::clearMap()
{
    _audioMap.clear();
    ADApi::ClearAllMedias();
}

int CAudioControl::doParseAttention(QString word)
{
    int status_code;
    QList<ADDataType> results = ADApi::ParseAttention(word, status_code, -10000, "", "http://120.76.205.173:8090/chat");
    qDebug() << "Voice: exec size " << results.size() ;
    for(int i=0; i<results.size(); i++)
    {
        ADDataType x = results.at(i);
        CVoiceObject *ad = _audioMap.value(x.media_id);
        if(ad!=NULL)
        {
            qDebug() << "Voice: exec " << ad->_type << " - " << x.attri
                        <<" - " << x.value << " - " << x.pre_unit;

            if(ad->_type == "air" && x.attri == "model" )
            {
                CAirDim *air_dim = (CAirDim *)ad->_device->_usrcontrol;
                CVoiceObject *air_mode = _audioMap.value(air_dim->_mode_id);
                if(air_mode!=NULL)
                {
                    air_mode->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
                }
            }
            else if(ad->_type == "air" && x.attri == "wind")
            {
                CAirDim *air_dim = (CAirDim *)ad->_device->_usrcontrol;
                CVoiceObject *air_wind = _audioMap.value(air_dim->_wind_id);
                if(air_wind!=NULL)
                {
                    air_wind->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
                }
            }
            else{
                ad->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
            }
        }
    }
}

int CAudioControl::doParseVoice(QByteArray json_array)
{
    qDebug() << "CAudioControl: doParseVoice ";
    int status_code;

    QList<ADDataType> xs = ADApi::ParseAttention(json_array, status_code);
    qDebug() << status_code;
    if(xs.size() != 0){
        for(int i=0;i<xs.size();i++) {
            ADDataType x = xs.at(i);

            qDebug() << "--------------------------";
            x.PrintOutInfo();
            qDebug() << "--------------------------";
            QThread::msleep(20);

            CVoiceObject *ad = _audioMap.value(x.media_id);
            if(ad!=NULL){
                if(ad->_type == "air" && x.attri == "mode1" )
                {
                    CAirDim *air_dim = (CAirDim *)ad->_device->_usrcontrol;
                    CVoiceObject *air_mode = _audioMap.value(air_dim->_mode_id);
                    if(air_mode!=NULL)
                    {
                        air_mode->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
                    }
                }
                else if(ad->_type == "air" && x.attri == "wind")
                {
                    CAirDim *air_dim = (CAirDim *)ad->_device->_usrcontrol;
                    CVoiceObject *air_wind = _audioMap.value(air_dim->_wind_id);
                    if(air_wind!=NULL)
                    {
                        air_wind->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
                    }
                }
                else{
                    ad->_device->_usrcontrol->execDeviceAction(x.attri,x.value,x.pre_unit);
                }
            }


        }
    }
    else
    {
        qDebug() << "CAudioControl: doParseVoice fail by " << status_code;
    }
    return status_code;
}

